{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Horizontal Current Loop over a Layered Earth (time domain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "from IPython.display import display\n",
    "from geoscilabs.em.TDEMHorizontalLoopCylWidget import TDEMHorizontalLoopCylWidget\n",
    "APP = TDEMHorizontalLoopCylWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import rcParams\n",
    "rcParams['font.size'] = 16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "Here, we show the transient fields and fluxes that result from placing a vertical magnetic dipole (VMD) source over a layered Earth. The transient response in this case refers to the fields and fluxes that are produced once a long-standing primary magnetic field is removed.\n",
    "\n",
    "There are [two commonly used models](https://em.geosci.xyz/content/maxwell1_fundamentals/dipole_sources_in_homogeneous_media/magnetic_dipole_time/index.html) for describing the VMD source that produces a transient response: 1) as an infinitessimally small bar magnet that experiences a long-standing vertical magnetization which is then instantaneously removed at $t=0$, and 2) as an infinitessimally small horizontal loop of wire carrying a constant current which is then instantaneously shut off at $t=0$ (step-off current waveform).\n",
    "\n",
    "True dipole sources do not exist in nature however they can be approximated in practice. For geophysical applications, we use small current loops to approximate transient VMD sources. These EM sources may be placed on the Earth's surface (ground-based surveys) or flown through the air (airborne surveys). According to the Biot-Savart law, a primary magnetic field is produced whenever there is current in the loop. When the current is shut-off, the sudden change in magnetic flux induces anomalous currents in the Earth which propagate and diffuse over time. The distribution and propagation of the induced currents depends on the subsurface conductivity distribution and how much time has passed since the current in the VMD source was shut off. The induced currents ultimately produce secondary magnetic fields which can be measured by a receiver.\n",
    "\n",
    "In this app, we explore the following:\n",
    "\n",
    "- How do the fields and currents produced by the transient VMD source change over time?\n",
    "- For a layered Earth, how does changing layer thickness and conductivity impact the fields and currents produced by the transient VMD source?\n",
    "- How do the secondary fields measured above the Earth's surface change over time?\n",
    "- For a layered Earth, how does changing layer thickness and conductivity impact secondary fields measured above the Earth's surface?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup\n",
    "\n",
    "The geological scenario being modeled is shown in the figure below. Here, we assume the Earth is comprised of 3 layers. Each layer can have a different electrical conductivity ($\\sigma$). However, a constant magnetic susceptibility ($\\chi$) is used for all layers; where $\\mu_0$ is the magnetic permeability of free space and $\\mu = \\mu_0 (1 +\\chi)$. The thicknesses of the top two layers are given by $h_1$ and $h_2$, respectively.\n",
    "\n",
    "In this case, a transient VMD source (*Tx*) is used to excite the Earth, and the Earth's TEM response (secondary magnetic field) is measured by a receiver (*Rx*). In practice, the transmitter and receiver may be placed near the Earth's surface or in the air. The receiver may also measure secondary fields at a variety of times after the source is shut off.\n",
    "\n",
    "To understand the fields and currents resulting from a transient VMD source we have two apps:\n",
    "\n",
    "- **Fields app:** Models the fields and currents everywhere at a particular time after shutoff\n",
    "- **Data app:** Models the secondary magnetic field observed by the receiver as a function of off-time\n",
    "\n",
    "\n",
    "<img src=\"https://github.com/geoscixyz/geosci-labs/blob/master/images/em/LayeredEarthTEM.png?raw=true\"></img>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise\n",
    "\n",
    "**Follow the exercise in a linear fashion. Some questions may use parameters set in a previous question.**\n",
    "\n",
    "- **Q1:** Set $\\sigma_1$, $\\sigma_2$ and $\\sigma_3$ to arbitrary conductivity values. Based on the geometry of the problem, which components (x, y, z) of each field (E, B, dBdt or J) are zero? Run the *Fields app* and set *AmpDir = None*. Next, try different combinations of *Field* and *Comp*. Does the simulation match what you initially thought?\n",
    "\n",
    "\n",
    "- **Q2:** Re-run the *Fields app* to set parameters back to default. What happens to the *Ey* and *Jy* as you increase *time index* starting at 1? How does the diffusion and propagation of the EM signal change if the conductivity of all the layers is increased to 1 S/m?\n",
    "\n",
    "\n",
    "- **Q3:** Re-run the *Fields app* to set parameters back to default. Set $\\sigma_1 = 0.01$ S/m, $\\sigma_2 = 1$ S/m and $\\sigma_3 = 0.01$ S/m. Now increase *time index* starting at 1. Is the signal able to effectively penetrate the conductive layer? Why/why not? What if the layer was resistive (i.e. $\\sigma_2 = 0.0001$ S/m) instead?\n",
    "\n",
    "\n",
    "- **Q4:** Repeat Q3 but examine the current density. Where is the highest concentration of current density at late time channels? Does this support your answer to Q3?\n",
    "\n",
    "\n",
    "- **Q5:** Re-run the *Fields app* to set parameters back to default. Set *Field = B*, *AmpDir = Direction*. What happens to the magnetic flux density as the *time index* is increased starting at 1? At (x,z)=(0,0), what is the vector direction of the magnetic flux density? Repeat Q5 for dBdt.\n",
    "\n",
    "\n",
    "- **Q6:** Re-run the *Fields app* to set parameters back to default. Set $\\sigma_1 = 0.01$ S/m, $\\sigma_2 = 1$ S/m and $\\sigma_3 = 0.01$ S/m. Examine how B and dBdt are impacted by the conductive layer."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fields app\n",
    "\n",
    "We use this app to simulate the fields and currents everywhere due to a transient VMD source. The fields and induced currents depend on time and the subsurface conductivity distribution. You will use the app to change various parameters in the model and see how the fields and currents change.\n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Update:** If *True* is selected, the simulation is re-run as parameters are being changed. If *False* is selected, the simulation will not be re-fun as parameters are being changed.\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": total magnetic flux density, \"dBdt\": time-derivative of the magnetic flux density, \"J\": current density and \"Model\": conductivity model)\n",
    "- **AmpDir:** If *None* is selected, then the *x*, *y* or *z* component chosen on the next line is plotted. If *Direction* is chosen, a vector plot is plotted (only possible for B and dB/dt)\n",
    "- **Comp.:** If *None* is selected on the previous line, the user chooses whether the *x*, *y* or *z* component is plotted.\n",
    "- Time index: The time channel at which fields are being plotted\n",
    "- $\\boldsymbol{\\sigma_0}$: Conductivity of 0th layer in S/m\n",
    "- $\\boldsymbol{\\sigma_1}$: Conductivity of 1st layer in S/m\n",
    "- $\\boldsymbol{\\sigma_2}$: Conductivity of 2nd layer in S/m\n",
    "- $\\boldsymbol{\\sigma_3}$: Conductivity of 3rd layer in S/m\n",
    "- $\\boldsymbol{\\chi}$: Susceptibility of 1-3 layers in SI\n",
    "- $\\boldsymbol{h_1}$: Thickness of the first layer in metres\n",
    "- $\\boldsymbol{h_2}$: Thickness of the second layer in metres\n",
    "- **Scale:** Plot data values on *log-scale* or *linear-scale*\n",
    "- $\\boldsymbol{\\Delta x}$ (m): Horizontal separation distance between the transmitter and receiver\n",
    "- $\\boldsymbol{\\Delta z}$ (m): Height of the transmitter and receiver above the Earth's surface\n",
    "- **Time index:** Time index for the set of frequencies models by this app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "de0c75b82f4b47e5b8800f46541c53d6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(Checkbox(value=True, description='Update'), ToggleButtons(description='Field', options=('E', '…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q1 = APP.InteractivePlane_Layer()\n",
    "display(Q1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data app\n",
    "\n",
    "Using this app, we show how the fields observed at the receiver location depend on the parameters set in the previous app. *Note that if you want to see changes in the data due to changes in the model, you MUST* re-run the previous app. \n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": magnetic flux density, \"dBdt\": time-derivative of the magnetic flux density)\n",
    "- **Comp.:** Direction of EM field at Rx locations        \n",
    "- **Scale:** Scale of y-axis values (\"log\" or \"linear\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c2ec5f3175c24a20ba5c208beeb506c4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(ToggleButtons(description='Field', index=1, options=('E', 'B', 'dBdt'), value='B'), ToggleButt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q2 = APP.InteractiveData_Layer()\n",
    "display(Q2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Explore\n",
    "\n",
    "EM fields will be depenent upon a number of parameters, using a simple half-space model ($\\sigma_1=\\sigma_2=\\sigma_3$) explore how EM fields and data changes upon below four parameters. \n",
    "\n",
    "- E1: Effects of frequency?\n",
    "\n",
    "\n",
    "- E2: Effects of Tx height?\n",
    "\n",
    "\n",
    "- E3: Effects of Conductivity?\n",
    "\n",
    "\n",
    "- E4: Effects of Susceptibility?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "widgets": {
   "state": {
    "c0dd4dbce2ff4d0cacf7363e6fdfed13": {
     "views": [
      {
       "cell_index": 6
      }
     ]
    },
    "d6ee822b25404d33979ba6ec5f19963c": {
     "views": [
      {
       "cell_index": 8
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
